<?php
namespace app\common\model;

class ShopAttrModel extends BaseModel
{
    protected $name = 'shop_attr';
    protected $autoWriteTimestamp = false;
    // 定义自动完成的属性
    protected $auto = []; //新增修改
    protected $insert = []; //插入
    protected $update = []; //修改

    /*读取器*/
    protected function getItemListAttr($value, $data)
    {
        $itemNameList = ShopAttrItemModel::where('attr_id', $data['attr_id'])->order('item_sort')->column('item_name');
        if (empty($itemNameList)) {
            return '';
        }
        return implode(',', $itemNameList);
    }
    /*修改器*/

    /**
     * 获取规格属性值列表
     * @param $attrIds
     * @return array|false|\PDOStatement|string|\think\Collection
     */
    public function getAttrList($attrIds)
    {
        $attrItemModel = new ShopAttrItemModel();
        $attrList      = $this->where('attr_id', 'in', $attrIds)->order('attr_sort')->select();
        $attrList      = $attrList->toArray();
        foreach ($attrList as $k => $v) {
            $itemList                  = $attrItemModel->where('attr_id', $v['attr_id'])->order('item_sort')->select();
            $attrList[$k]['item_list'] = $itemList->toArray();
        }
        return $attrList;
    }
}
